require(['./config'], () => {
  require(['request','template','header','footer','jquery','bootstrap'], ( request,template,header ) => {
  class Category {
    constructor () {
      this.getList().then(() => {
        this.select()
        this.addSort()
      })
    }
    // 渲染列表
    getList() {
      // 1.获取地址栏的ID
      // 2.根据ID获取请求渲染数据
      const id = location.search.slice(4)
      return new Promise(resolve => {
        request.getList(id).then(resp => {
          if(resp.code === 200) {
            const {list} = resp.data.items
            this.list = list
            const { cname } = resp.data.category
            this.cname = cname
            $('#list_tabs').html(template('list_tabs_template',{ cname, yeMan: list}))
            resolve()
          }
        })
      }) 
    }
    // 选择价格10到20元
    select() {
      console.log(this.list)
      $('#py').on('click', ()=> {
        var list = this.list.filter(item => {
          return item.price < 20 && item.price > 10
        })
        $('#list_tabs').html(template('list_tabs_template',{ cname:this.cname, yeMan: list}))
      })
    }
    // 升序排列
    addSort() {
      $('#shenXu').on('click', ()=> {
        let list = this.list.sort(compare('price'))
        function compare(property){
          return function(a,b){
              var value1 = a[property];
              var value2 = b[property];
              return value1 - value2;
          }
      }
        $('#list_tabs').html(template('list_tabs_template',{ cname:this.cname, yeMan: list}))
      })
    }
  }
  new Category() 
  })
})